<?php

/**
 * Manage some base actions
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class LoginController extends Controller {

    public $layout = 'column1';
    public $content = array();

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
          // captcha action renders the CAPTCHA image displayed on the contact page
          'captcha' => array(
            'class' => 'CCaptchaAction',
            'backColor' => 0xFFFFFF,
          ),
          'newsletter' => array(
            'class' => 'application.widgets.newsletter.ANewsletter'
          )
        );
    }

    /**
     * render home page
     */
    public function actionIndex() {
        //$this->description = settings()->get('seo', 'home_description');
        //$this->keywords = settings()->get('seo', 'home_keywords');
        //page title
        //$pageTitle = settings()->get('seo', 'home_title');
        //if ($pageTitle != NULL) {
        //    $this->pageTitle = $pageTitle;
        //}
        if (r()->isPostRequest) {
            $userRegistration = new mRegistration();
            $userRegistration->aliasAttrFormGreetingWithModel($_POST);
            $userRegistration->updateGreeting();
        }
        $this->render('login', array('csrftoken' => Yii::app()->request->csrfToken));
        //$this->render('login', array('model' => $model));
    }

    /*
     * Description: Submit Login it run this function
     */

    public function actionProcess() {
        if (r()->isPostRequest) {
            if (Yii::app()->request->csrfToken == $_POST['ioBlackBox']) {
                //echo User::checkLogin($_POST['username'], $_POST['password']);
                //exit();
                $model = new LoginForm;
            }
        }
    }

    public function actionLogin() {
        if (!defined('CRYPT_BLOWFISH') || !CRYPT_BLOWFISH)
            throw new CHttpException(500, "This application requires that PHP was compiled with Blowfish support for crypt().");

        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect('user/profile');
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

}
